<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../_inc/funct.cerBil.php');
require_once('../../_inc/funct.tabDati.php');

$aVar = array();
$aDefDati = array();
aVarLoadBil(reqVar('id'), $aVar,$aDefDati,'B');
//DBG_("CARICA DATI INPUT");
foreach($aVar as $codX => $valX) {
	if ($codX[0]=='B') {
		$aVar['Q'.substr($codX,1)] = $aVar[$codX];
	}
}

$aGeDati = getGerarcDefDati($aDefDati,1);


$sSql = "SELECT d.cod_dato, script_php FROM rgl_def_dato d "
				." WHERE id_set_dati = ".$aVar['B_ID_SET_DATI']
				." AND tipo_tab_dati = 1 "
				." AND d.script_php <> '' "
				." ORDER BY d.livello";
$objDB->exec($sSql);
//dbg_($sSql);
$aCalc = array();
foreach($aDefDati as $codX => $acX) {
	if ($acX['tipo_tab_dati'] != 1 || $acX['script_php'] == '') continue;
	$aCalc[$acX['livello']][$codX] = $acX['script_php'];
}
ksort($aCalc);
$bQuadra = true;
foreach($aCalc as $liv =>$aLiv) {
	foreach($aLiv as $codX => $scriptPhp) {
		$nomeFunz = "rglScrDato".$codX;
		if (!function_exists($nomeFunz)) {
			//dbg_($scriptPhp);
			eval($scriptPhp);
			if (!function_exists($nomeFunz)) {
				$msgPag .= "Script $nomeFunz (".$aVar['B_ID_SET_DATI'].") anomalo<br>";
			}
		}
		$aVar["_SCRIPT_MESS"] = '';
		$rc = $nomeFunz($aVar);
		if (!isset($aVar['Q_'.$codX])) {
			$aVar['Q_'.$codX] = 0;
		}
		//dbg_($acX['cod_dato']."=".$aVar['Q_'.$acX['cod_dato']]);
		$msgPag .= trim($aVar["_SCRIPT_MESS"])."\n";
		if (trim($aVar["_SCRIPT_MESS"])!='') $bQuadra = false;
		if ($aVar['Q_'.$codX] != $aVar['B_'.$codX]) $bQuadra = false;
	}
}

$msgPag = nl2br(trim($msgPag));
if ($bQuadra) {
	$msgPag .= "=== QUADRA ===";
} else {
	$msgPag .= "<br>*** NON QUADRA ***";
}
//dbg_($msgPag);
$sCmdL = '';

$sCmdR = '';
//$sCmdR .= "<a class='cmd1' href=''>...</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('', $msgPag, 'ratg');
?>
<script type="text/javascript">
<!--
function submAz(az) {
	document.frm1.azione.value = az;
	document.frm1.submit();
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo getHtmlForm('frm1','',array('enctype'=>true));
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="lab1" align="center" colspan='20'><b>Azienda</b></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Ragione sociale</td>
      <td class="txt1" align="left" colspan='9'><?=$aVar['B_RAG_SOC']?></td>
      <td class="lab1" align="left" colspan='3'>Cod CCIAA</td>
      <td class="txt1" align="left" colspan='5'><?=$aVar['B_COD_CCIAA']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Sede</td>
      <td class="txt1" align="left" colspan='9'><?=$aVar['B_SEDE']?></td>
      <td class="lab1" align="left" colspan='3'>Cod.fisc.</td>
      <td class="txt1" align="left" colspan='5'><?=$aVar['B_COD_FISC']?></td>
   </tr>
   <tr>
      <td class="lab1" align="left" colspan='3'>Cod. sett.</td>
      <td class="txt1" align="left" colspan='2'><?=$aVar['B_COD_SETT']?></td>
      <td class="txt1" align="left" colspan='15'><?=$aVar['B_DESCR_SETT']?></td>
   </tr>
	<?
$aOptB = array("onChange"=>"submAz('reload');");
?>
	<tr>
		<td class='lab1' colspan='3' align='left'>Bilancio corrente</td>
		<td class='txt1' align='left' colspan='17'>
			<?=YMD2DMY($aVar['B_DATA_RIF'])." (".$aVar['B_STATO_LAV'].") - ".$aVar['B_DESCR_BIL'];?>
		</td>
	</tr>
	<tr>
		<td class='lab1'align='center' colspan='20'><b>BILANCIO</b></td>
	<tr>
<?
if (trim($aVar['B_NOTA'])!='') {
?>
	<tr>
		<td class='txt1'align='left' colspan='20'>
			<?=getHtmlViewNote($aVar['B_NOTA']);?>
		</td>
	<tr>
<?
}
foreach($aGeDati as $codX => $aX) {
	echoDato($codX,$aX,$aVar,0);
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td> 
   </tr>
</table>
<?
echo "</form>\n";

codaHtml();
exit();


function echoDato($codX, $aX, &$aVar, $liv) {
	global $objSess, $objDB;
	$aCSez = array(''=>'-- non sezione --');
	$aCSez['spatt'] = 'Stato patrimoniale - Attivo';
	$aCSez['sppas'] = 'Stato patrimoniale - Passivo';
	$aCSez['cecon'] = 'Conto economico';
	$aCSez['altro'] = 'Altri dati';
	$aCSez['calco'] = 'Dati calcolati';
	
	if ($aX['attr']['cod_sez']!='') {
		echo "<tr><td class='txt1'align='center' colspan='13'><b>".$aCSez[$aX['attr']['cod_sez']]."</b></td>\n"
			."</td><td class='txt1' align='center' colspan='1'><i>flag</i></td>\n"
			."</td><td class='txt1' align='center' colspan='2'><i>".YMD2DMY($aVar['B_DATA_RIF'])."</i></td>\n"
			."</td><td class='txt1' align='center' colspan='2'><i>Calcolato</i></td>\n"
			."</td><td class='txt1' align='center' colspan='2'><i>Diff.</i></td>\n<tr>\n";
	}
	$numX = (isset($aVar['B_'.$codX])) ? $aVar['B_'.$codX] : 0;
	$valX = ($numX==0)?'':number_format($numX,2,',','.');
	$numY = (isset($aVar['Q_'.$codX])) ? $aVar['Q_'.$codX] : 0;
	$valY = ($numY==0)?'':number_format($numY,2,',','.');
	if ($aX['attr']['flag_quad_calc']=='totmax') {
		$diff = ($numX>=$numY)?'':number_format(($numY-$numX),1,',','.');
	} else {
		$diff = ($numX==$numY)?'':number_format(($numY-$numX),1,',','.');
	}
		echo "<tr><td class='txt1' colspan='13'>".str_repeat("&nbsp;",$liv*8)
			."<a href='datoVis.php?cd=$codX&xsn=".XSN."' class='txt1'>$codX</a> - ".$aX['attr']['descr']
			."</td><td class='txt1' align='right' colspan='1'>".$aX['attr']['flag_quad_calc']."</td>"
			."</td><td class='txt1' align='right' colspan='2'>$valX</td>"
			."</td><td class='txt1' align='right' colspan='2'>$valY</td>"
			."</td><td class='txt1' align='right' colspan='2'><b>$diff</b></td><tr>\n";
	foreach($aX['figli'] as $codY => $aY)  {
		echoDato($codY, $aY, $aVar, $liv+1);
	}
}

